package tk.mybatis.springboot.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.springboot.model.*;
import tk.mybatis.springboot.result.Result;
import tk.mybatis.springboot.service.UserService;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.net.URLEncoder;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;

    @GetMapping("/login")
    public Result<Object> login(@RequestParam("code") String code) {
        Students students = userService.login(code);
        return  Result.success(students);
    }



    @PostMapping("/getLoginUser")
    public Result<Object> getLoginUser(@RequestBody Students students) {
        Students result = userService.getLoginUser(students);
        return  Result.success(result);
    }

    @PostMapping("/getStudents")
    public Result<Object> getStudents(@RequestBody StudentsForm students) {
        return  Result.success(userService.getStudents(students,false));
    }

    @PostMapping("/getStudents2")
    public Result<Object> getStudents2(@RequestBody StudentsForm students) {
        return  Result.success(userService.getStudents(students,true));
    }
    @PostMapping("/register")
    public Result<Object> register(@RequestBody Students students) throws Exception {
        Students result = userService.register(students);
        return  Result.success(result);
    }


    @PostMapping("/setting")
    public Result<Object> setting(@RequestBody Students students) {
        Students result = userService.setting(students);
        return  Result.success(result);
    }

    @PostMapping("/getClassAndGroup")
    public Result<Object> getClassAndGroup(@RequestBody Students students) {
        List<StudentCourse> result = userService.getClassAndGroup(students.getId());
        return  Result.success(result); 
    }

    @PostMapping("/getWorks")
    public Result<Object> getWorks(@RequestBody Course course) {
        return  Result.success(userService.getWorks(course.getId()));
    }

    @PostMapping("/getResource")
    public Result<Object> getResource(@RequestBody GetResourceBody body) {
        return  Result.success(userService.getResource(body));
    }

    @PostMapping("/setCourse")
    public Result<Object> setCourse(@RequestBody StudentCourse studentCourse) {
        return  Result.success(userService.setCourse(studentCourse));
    }

    @PostMapping("/setAward")
    public Result<Object> setAward(@RequestBody AwardRequest awardRequest) {
        return Result.success(userService.setAward(awardRequest));
    }

    @PostMapping("/getAward")
    public Result<Object> getAward(@RequestBody GetAwardBody getAwardBody) {
        return Result.success(userService.getAward(getAwardBody));
    }

    @PostMapping("/getCertificate")
    public Result<Object> getCertificate(@RequestBody GetAwardBody getAwardBody) {
        return Result.success(userService.getCertificate(getAwardBody));
    }

    @PostMapping("/setFace")
    public Result<Object> setFace(@RequestBody Students students) {
        userService.setFace(students);
        return Result.success();
    }

    @PostMapping("/getWork")
    public Result<Object> getWork(@RequestBody Homework homework) {
        return Result.success(userService.getWork(homework));
    }

    @PostMapping("/setWork")
    public Result<Object> setWork(@RequestBody HomeworkParams params) {
        userService.setWork(params);
        return Result.success();
    }

    @PostMapping("/getWorkToday")
    public Result<Object> getWorkToday(@RequestBody StudentCourse students) {
        return Result.success( userService.getWorkToday(students));
    }


    @PostMapping("/getNoWork")
    public Result<Object> getNoWork(@RequestBody StudentCourse students) {
        return Result.success( userService.getNoWork(students));
    }

    @PostMapping("/overWork")
    public Result<Object> overWork(@RequestBody StudentWork studentWork) {
        userService.overWork(studentWork);
        return Result.success();
    }



    @PostMapping("/getWorksBySession")
    public Result<Object> getWorksBySession(@RequestBody Homework homework) {
        return Result.success( userService.getWorksBySession(homework));
    }

    @PostMapping("/addHomeWork")
    public Result<Object> addHomeWork(@RequestBody HomeworkInfo homeworkInfo) {
        userService.addHomeWork(homeworkInfo);
        return Result.success();
    }

    @PostMapping("/getOtherWork")
    public Result<Object> getOtherWork(@RequestBody HomeworkInfo homeworkInfo) {
        return Result.success( userService.getOtherWork(homeworkInfo));
    }

    @PostMapping("/like")
    public Result<Object> like(@RequestBody HomeworkInfo homeworkInfo) {
        userService.like(homeworkInfo);
        return Result.success();
    }

    @PostMapping("/addComment")
    public Result<Object> addComment(@RequestBody Comment comment) {
        userService.addComment(comment);
        return Result.success();
    }

    @PostMapping("/getVideo")
    public Result<Object> getVideo(@RequestBody CourseSession session) {
        return Result.success(userService.getVideo(session));
    }

    @PostMapping("/setVideo")
    public Result<Object> setVideo(@RequestBody CourseSession session) {
        userService.setVideo(session);
        return Result.success();
    }


    @PostMapping("/setTimes")
    public Result<Object> setTimes(@RequestBody CourseSession session) {
        userService.setTimes(session);
        return Result.success();
    }

    @PostMapping("/getDownloadImg")
    public void getDownloadImg(@RequestBody Certificate certificate, HttpServletResponse response) throws Exception {
        // 设置响应头
        response.setContentType("image/png");
        response.setHeader("Content-Disposition",
                "attachment; filename=" + URLEncoder.encode("certificate.png", "UTF-8"));

        // 创建图片（800x800像素）
        BufferedImage image = new BufferedImage(800, 800, BufferedImage.TYPE_INT_RGB);
        Graphics2D g = image.createGraphics();

        try {
            // 设置抗锯齿
            g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB);

            // 绘制背景图
            try {
                BufferedImage background = ImageIO.read(getClass().getResourceAsStream("/static/img/certificate-bg.png"));
                Image scaledBackground = background.getScaledInstance(800, 800, Image.SCALE_SMOOTH);
                g.drawImage(scaledBackground, 0, 0, null);
            } catch (Exception e) {
                g.setColor(Color.WHITE);
                g.fillRect(0, 0, 800, 800);
                System.err.println("背景图加载失败，使用默认背景: " + e.getMessage());
            }

            // 设置字体
            Font nameFont = new Font("宋体", Font.BOLD, 28);
            Font contentFont = new Font("宋体", Font.PLAIN, 24);
            Font certIdFont = new Font("宋体", Font.PLAIN, 20);
            Font signatureFont = new Font("华文行楷", Font.BOLD, 26);

            // 设置文本颜色为黑色
            g.setColor(Color.BLACK);

            // 绘制称呼（左对齐）
            g.setFont(nameFont);
            String greeting = certificate.getName() + " 同学：";
            g.drawString(greeting, 80, 300);

            // 绘制内容（左对齐，分行）
            g.setFont(contentFont);
            String text = "您已完成" + certificate.getClassName() + "课程学习,成绩合格，准予结业。";
            g.drawString(text, 150, 350);

            // 绘制证书编号（左对齐）
            g.setFont(certIdFont);
            String certIdText = "证书编号: " + certificate.getId();
            g.drawString(certIdText, 80, 500);

            // 绘制日期和落款
            g.setFont(certIdFont);
            String dateText = certificate.getCreateTime();
            g.drawString(dateText, 80, 550);

            g.setFont(certIdFont);
            String signature = "每日国学小程序官方";
            g.drawString(signature, 500, 550);

            // 保存图片到响应输出流
            ImageIO.write(image, "PNG", response.getOutputStream());
        } finally {
            g.dispose();
        }
    }
}